package com.amex.uam.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Utils {
	public static final boolean DEBUG = true;
	public static final SimpleDateFormat FORMATTER_US = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	public static final SimpleDateFormat FORMATTER_US_DATE = new SimpleDateFormat("MM/dd/yyyy");
	
	public static String toJson(Object src) {
		Gson gson = null;
		if (DEBUG) {
			gson = new GsonBuilder().setPrettyPrinting().create();
		} else {
			gson = new Gson();
		}
		return gson.toJson(src);
	}

	public static String trim(String source, String def) {
		if (source == null) {
			return def;
		}
		return source.trim();

	}

	public static boolean isEmpty(String s) {
		return (s == null) || (s.trim().length() == 0);
	}
	
	public static int parseInt(String source, int def) {
		try {
			return Integer.parseInt(source.trim());
		}catch(Throwable t){
			return def;
		}
	}
	
	public static String formatDatetime(Date date){
		if (date == null) return null;
		return FORMATTER_US.format(date);
	}
	
	public static String formatDate(Date date){
		if (date == null) return null;
		return FORMATTER_US_DATE.format(date);
	}
	
	public static Date parseDatetime(String date) {
		if (date == null) return null;
		try {
			return FORMATTER_US.parse(date);
		} catch (ParseException e) {
			return null;
		}
	}
	
	public static Date parseDate(String date) {
		if (date == null) return null;
		try {
			return FORMATTER_US_DATE.parse(date);
		} catch (ParseException e) {
			return null;
		}
	}
	
	public static Timestamp parseTimestamp(String date) {
		Date d = parseDate(date);
		if (d != null) {
			return new Timestamp(d.getTime());
		}
		return null;
	}
	
	public static void main(String[] args) {
		System.out.println(parseDate("09/27/2014 10:20:20"));
		System.out.println(formatDate(new Date()));
	}
}
